package com.suozq.springcloud.gateway.config;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import reactor.core.publisher.Mono;

@Component
public class PermissionGlobalFilter implements GlobalFilter,Ordered{

	@Override
	public int getOrder() {
		return 0;
	}

	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		String contextPath = exchange.getRequest().getPath().value();
		if(contextPath.contains("/login")) {
			return chain.filter(exchange);
		}
		Object tokenObj = exchange.getRequest().getHeaders().get("accessToken");
		if(tokenObj==null) {
			exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
			return exchange.getResponse().setComplete();
		}
		return chain.filter(exchange);
	}

}
